@file:Suppress("DEPRECATED_IDENTITY_EQUALS")

package com.github.xuejike.springboot.generator
import com.baomidou.mybatisplus.generator.InjectionConfig
import com.baomidou.mybatisplus.generator.config.*
import com.baomidou.mybatisplus.generator.config.po.TableInfo
import com.baomidou.mybatisplus.generator.config.rules.IColumnType
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine
import javassist.compiler.CodeGen
import java.util.*
import java.io.FileInputStream
import java.io.BufferedInputStream
import kotlin.collections.HashMap


var fieldMap=HashMap<String,IColumnType>()

fun main(args: Array<String>){

}

fun typeHandler(tableName:String,columnName:String):IColumnType?{
    return null;
}
fun codeGen(prop: Properties) {
    val mpg = JpaAutoGenerator()




    // 全局配置
    val gc = GlobalConfig()
    val projectPath = prop.getProperty("projectPath")
//    gc.setOutputDir(".tpl\\spring-jpa-demo\\src\\main\\java")

    gc.setOutputDir(projectPath+"\\src\\main\\java");
    gc.setAuthor(prop.getProperty("author"))
    gc.setOpen(false)
    gc.setFileOverride(true);
    mpg.setGlobalConfig(gc)

    // 数据源配置
    val dsc = DataSourceConfig()
    dsc.setUrl(prop.getProperty("dataSourceUrl"))
    dsc.setDriverName(prop.getProperty("driverName"))
    dsc.setUsername(prop.getProperty("userName"))
    dsc.setPassword(prop.getProperty("password"))
    mpg.setDataSource(dsc)

    // 包配置
    val pc = PackageConfig()
    pc.setModuleName(prop.getProperty("moduleName"))
    pc.setParent(prop.getProperty("parent"))
    mpg.setPackageInfo(pc)

    // 自定义配置
    val cfg = object : InjectionConfig() {
        override fun initMap() {
            // to do nothing
        }
    }

    var templatePath = "/jpa/view.ftl"
    // 自定义输出配置
    val focList = ArrayList<FileOutConfig>()

    focList.add(object : FileOutConfig(templatePath){
        override fun outputFile(tableInfo: TableInfo): String {
            // 自定义输出文件名
            return (projectPath + "/src/main/kotlin/"+pc.parent.replace(".","/")+"/" + pc.moduleName
                    + "/controller/view/" + tableInfo.entityName + ".kt")
        }
    })


    cfg.setFileOutConfigList(focList)
    mpg.setCfg(cfg)

    // 配置模板
    val templateConfig = TemplateConfig()

    // 配置自定义输出模板
    //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
     templateConfig.setEntity("jpa/entity.java");
    templateConfig.setMapper("jpa/mapper.java")
     templateConfig.setService("jpa/service.java");
     templateConfig.setController("jpa/controller.java");
     templateConfig.setServiceImpl("jpa/serviceImpl.java");
    templateConfig.setXml(null)
    mpg.setTemplate(templateConfig)

    // 策略配置
    val strategy = StrategyConfig()
    strategy.setNaming(NamingStrategy.underline_to_camel)
    strategy.setColumnNaming(NamingStrategy.underline_to_camel)
//    strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity")
    strategy.setEntityLombokModel(true)
    strategy.setRestControllerStyle(false)
    strategy.setSuperMapperClass("org.springframework.data.jpa.repository.JpaRepository")
    strategy.setSuperControllerClass("com.github.xuejike.springboot.app.common.BaseController")
    strategy.setSuperServiceClass("com.github.xuejike.springboot.app.common.IService")
    strategy.setSuperServiceImplClass("com.github.xuejike.springboot.app.common.ServiceImpl")
    strategy.setInclude("zy_slide_show_image")


    strategy.setSuperMapperClass(prop.getProperty("superMapperClass"))
    strategy.setSuperControllerClass(prop.getProperty("superControllerClass"))
    strategy.setSuperServiceClass(prop.getProperty("superServiceClass"))
    strategy.setSuperServiceImplClass(prop.getProperty("superServiceImplClass"))
    strategy.setInclude(prop.getProperty("include"))
    strategy.setEntityTableFieldAnnotationEnable(true)
    strategy.setTablePrefix(prop.getProperty("tablePrefix"))
    strategy.setEntityTableFieldAnnotationEnable(true)
    strategy.setControllerMappingHyphenStyle(true)
    mpg.setStrategy(strategy)
    mpg.setTemplateEngine(FreemarkerTemplateEngine())
    mpg.execute()
}